package helpers;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.Locale;

public class DateHelper {
	public static List<Long> calDistanceTimeToNow(Date date) {
		List<Long> arr = new ArrayList<Long>();
		Date now = new Date();
		long seconds = (now.getTime() - date.getTime())/1000;
		long day = (int) (seconds / 86400);
		long hour = (int) ((seconds - day*86400) / 3600);
		long minute = (int) ((seconds - day*86400 - hour*3600) / 60);
		arr.add(day);
		arr.add(hour);
		arr.add(minute);
		arr.add(seconds);
		return arr;
	}
	
	public static List<Long> coundownFromNow(Date date) {
		List<Long> arr = new ArrayList<Long>();
		Calendar cal=Calendar.getInstance();
		Date now = new Date();
		cal.setTime(date);
		cal.add(Calendar.DATE, +3);
		
		long seconds = (cal.getTime().getTime() - now.getTime())/1000;
		long day = (int) (seconds / 86400);
		long hour = (int) ((seconds - day*86400) / 3600);
		long minute = (int) ((seconds - day*86400 - hour*3600) / 60);
		
		arr.add(day);
		arr.add(hour);
		arr.add(minute);
		arr.add(seconds);
		return arr;
	}
	
	public static Boolean checkGreaterNow(Date date, int minutes) {
		Boolean isGreater = false;
		Date now = new Date();
		if (now.getTime() - date.getTime() - minutes * 60000 > 0) {
			isGreater = false;
		} else {
			isGreater = true;
		}
		return isGreater;
	}
	
	public static Date setDate(String strDate, String formatDate) {
		Calendar cal = Calendar.getInstance();
		DateFormat dateformat = new SimpleDateFormat("dd-MM-yyyy h:m:s");
		if (formatDate != null) {
			dateformat = new SimpleDateFormat(formatDate);
		}
		try {
			cal.setTime(dateformat.parse(strDate));
		} catch (Exception e) {
			e.printStackTrace();
		}
		return cal.getTime();
	}
	
	public static Calendar setCalendar(String strDate, String formatDate) {
		Calendar cal = Calendar.getInstance();
		DateFormat dateformat = new SimpleDateFormat("dd-MM-yyyy h:m:s");
		if (formatDate != null) {
			dateformat = new SimpleDateFormat(formatDate);
		}
		try {
			cal.setTime(dateformat.parse(strDate));
		} catch (Exception e) {
			e.printStackTrace();
		}
		return cal;
	}
	
	public static String getStringFromDate(Date date, String formatDate) {
		DateFormat dateformat = new SimpleDateFormat("dd-MM-yyyy h:m:s", Locale.ENGLISH);
		if (formatDate != null) {
			dateformat = new SimpleDateFormat(formatDate, Locale.ENGLISH);
		}
		
		String strDatePrize = (String) dateformat.format(date.getTime());
		return strDatePrize;
	}
}
